﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Alarm
{
    // 定义嘀嗒事件委托
    public delegate void TickEventHandler(object sender, EventArgs e);

    // 定义响铃事件委托
    public delegate void AlarmEventHandler(object sender, EventArgs e);

    public class AlarmClock
    {
        private int alarmTime; // 响铃时间（秒）
        private int currentTime; // 当前时间（秒）

        public event TickEventHandler Tick;
        public event AlarmEventHandler Alarm;

        public AlarmClock(int alarmTime)
        {
            this.alarmTime = alarmTime;
            this.currentTime = 0;
        }

        // 启动闹钟
        public void Start()
        {
            while (currentTime <= alarmTime)
            {
                // 触发嘀嗒事件
                OnTick();
                Thread.Sleep(1000); // 模拟每秒一次嘀嗒
                currentTime++;

                if (currentTime == alarmTime)
                {
                    // 触发响铃事件
                    OnAlarm();
                }
            }
        }

        // 触发嘀嗒事件的方法
        protected virtual void OnTick()
        {
            Tick?.Invoke(this, EventArgs.Empty);
        }

        // 触发响铃事件的方法
        protected virtual void OnAlarm()
        {
            Alarm?.Invoke(this, EventArgs.Empty);
        }
    }

    class Program
    {
        static void Main()
        {
            // 设置闹钟响铃时间为 5 秒后
            AlarmClock alarm = new AlarmClock(5);

            alarm.Tick += (sender, e) =>
            {
                Console.WriteLine("嘀嗒...");
            };

            alarm.Alarm += (sender, e) =>
            {
                Console.WriteLine("闹钟响铃！");
            };

            alarm.Start();
        }
    }
}
